﻿using MediatR;
using Microsoft.EntityFrameworkCore;
using SlaughterItem.Common.Domains.ListWeek2;
using SlaughterItem.Common.Dtos;
using SlaughterItem.Common.Infrastructures.Interfaces;
using SlaughterItem.Enum;

namespace SlaughterItem.API.Application.Commands.Week02.Leo
{
    public class SlaughteredUpdateHandler : IRequestHandler<SlaughteredUpdateCommend, Result>
    {
        private readonly IBaseRepository<Slaughteredtes> Slaughteredtes;

        public SlaughteredUpdateHandler(IBaseRepository<Slaughteredtes> Slaughteredtes)
        {
            this.Slaughteredtes = Slaughteredtes;
        }

        public async Task<Result> Handle(SlaughteredUpdateCommend request, CancellationToken cancellationToken)
        {
            var str = new Result() { Code = (int)CommonErrorCode.Update };

            try
            {
                var list = await Slaughteredtes.GetAll().Where(x => x.SlaughteredId == request.SlaughteredId).FirstAsync();
                if (list !=null)
                {
                    list.SlaughteredId = request.SlaughteredId;
                    list.MarkId = request.MarkId;
                    list.LiveAnimalsId = request.LiveAnimalsId;
                    list.WaitingId = request.WaitingId;
                    list.SlaughteredTime = request.SlaughteredTime;
                    list.SlaughteredRemark= request.SlaughteredRemark;

                }
                var info = Slaughteredtes.Update(list);
                if (info !=null)
                {
                    str.Code = (int)CommonErrorCode.Success;
                    str.Msg = "移圈成功";
                }
            }
            catch (Exception)
            {

                throw;
            }
            return str;
        }
    }
}
